package com.example.dingtalklogin.adpter.web;

import com.example.dingtalklogin.common.util.RequestHolder;
import com.example.dingtalklogin.common.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 * @author xuzhen
 */
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (UserUtil.getCurrUser() != null) {
            return true;
        } else {
            if(log.isDebugEnabled()){
                log.debug("请求url: {}。",request.getRequestURI());
            }
            // 跳转到自动登录
            response.sendRedirect("/login?path="+ RequestHolder.encodeUrl(request.getRequestURI()));
            return false;
        }
    }

}